import { ReviewRolesService } from 'src/app/@core/services/ReviewRoles/review-roles.service';
import { Component } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
  selector: 'app-review-flow-node',
  templateUrl: './review-flow-node.component.html',
  styleUrls: ['./review-flow-node.component.scss']
})
export class ReviewFlowNodeComponent {
  nodes =[];
  options =[];
  display = false;
  selectNode = this.nodes[0];
  id:any;
  msgs: { severity: any; summary: any; content: any; }[];
  isBarCode: boolean = false;
  canAddNode: boolean = true;
  constructor(private ReviewRolesService:ReviewRolesService,private route: ActivatedRoute,private router:Router,){}
  ngOnInit(): void {
    this.ReviewRolesService.GetPagedApprovalGroup(1,9999).subscribe((data:any)=>{
      this.options=data.approvalGroups;
    })
    this.route.params.subscribe(params => {
      this.id = params.id; // 打印查询参数
      this.ReviewRolesService.GetApprovalFlowConfig(this.id).subscribe((data:any)=>{
        this.nodes=data.approvalNodes;
      })
    });
  }
  addNode(node?: any) {
    if (this.canAddNode || this.nodes.length === 0) {
      const newNode = { order: 1, description: '', approvalGroupId: 0, approvalGroupName: '' };

      if (!node) {
        this.nodes.unshift(newNode);
      } else {
        const index = this.nodes.indexOf(node);
        this.nodes.splice(index + 1, 0, newNode);
      }

      this.nodes.forEach((item, index) => {
        item.order = index + 1;
      });
    }else{
      if(this.nodes.length>=1){
        this.showToast('error','错误',"扫码审核只能存在一个节点")
      }
    }


  }
  deleteNode(node?:any){
    this.nodes = this.nodes.filter(item=>item.order!==node.order)
    this.nodes.forEach((item, index) => {
      item.order = index + 1;
    });
    this.display = false;
  }
  selectGroup($event:any){
    this.selectNode.approvalGroupId=$event.approvalGroupId;
    this.selectNode.approvalGroupName=$event.name;
  }
  editNode(node?:any){
    this.display = true;
    this.selectNode = node;
  }
  changeIsBarCode() {
    if (this.isBarCode) {
      // 如果 isBarCode 为 true，确保只有一个节点
      if (this.nodes.length > 1) {
        this.nodes = [this.nodes[0]]; // 保留第一个节点，移除其他节点
      }
      // 禁用新增节点的功能
      this.canAddNode = false;
    } else {
      // 启用新增节点的功能
      this.canAddNode = true;
    }
  }
  saveNode(){
    const putData = {
      approvalFlowConfigId: Number(this.id),
      nodes:this.nodes
    }
    this.ReviewRolesService.DesignApprovalFlow(putData).subscribe((putData:any)=>{
      this.ReviewRolesService.GetApprovalFlowConfig(this.id).subscribe((data:any)=>{
        this.nodes=data.approvalNodes;
        this.showToast('success','成功',"审核流节点配置成功")
        setTimeout(() => {
          this.cancle();
        }, 1000);
      })
    },(error: any) => {
      if (error.status === 404) {
        this.showToast('error', '错误', '网络连接错误');
      } else {
        this.showToast('error', '错误', error.error);
      }
    }
  )
  }
  showToast(type: any,summary:any,content:any) {
    switch (type) {
    default:
      this.msgs = [{ severity: type, summary: summary, content: content }];
    }
  }//提示框
  cancle(){
    this.router.navigate(['/pages/systemset/Review-Flow']);
  }
}
